from pylatexenc.latex2text import latex2text

# 遍历从 1 到 80
for i in range(1, 81):
    chapter_num = str(i).zfill(2)
    input_file = f"../seg/chapter{chapter_num}_seg.tex"
    output_file = f"chapter{chapter_num}.txt"
    try:
        # 读取 LaTeX 文件内容
        with open(input_file, 'r', encoding='utf-8') as f:
            latex_content = f.read()
        # 将 LaTeX 内容转换为纯文本
        plain_text = latex2text(latex_content)
        # 将转换后的纯文本写入新文件
        with open(output_file, 'w', encoding='utf-8') as out_f:
            out_f.write(plain_text)
        print(f"成功将 {input_file} 转换为 {output_file}")
    except FileNotFoundError:
        print(f"未找到文件 {input_file}")
    except Exception as e:
        print(f"处理 {input_file} 时出现错误: {e}")

